package restaurant;

import javax.swing.*;
import javax.swing.border.TitledBorder;

import java.awt.event.*;
import java.awt.*;
/**
 * GUI class contains the graphical user interface between the user and the
 * functionality of the application as well as controlling all functionality
 * from using the manager class.
 * 
 * @author CT267, SG350, MSM41.
 */
@SuppressWarnings("serial")
public class GUI extends JFrame implements ActionListener {
	// private JLabel Label1;
	private JButton Frequency, Summary, SummaryByTable, NotOrdered;
	private JTextField textField;
	private JTextArea Results;
	private JScrollPane Scroll;
	private JPanel NorthPanel, MiddlePanel, SouthPanel;
	private GUI MGUI;
	Manager manager = new Manager();

	/**
	 * GUI Initialisation.
	 */
	public void MainGUI() {
		// GUI Layout
		MGUI = new GUI();
		setLayout(new BorderLayout(5, 5));

		MGUI.setTitle("Java Restaurant");
		// Buttons
		NotOrdered = new JButton("NotOrdered");
		NotOrdered.addActionListener(this);

		Frequency = new JButton("Frequency");
		Frequency.addActionListener(this);
		Summary = new JButton("Summary");
		Summary.addActionListener(this);

		SummaryByTable = new JButton("Summary By Table");
		SummaryByTable.addActionListener(this);
		// Buttons

		// Labels
		// Label1 = new JLabel();

		// Text
		textField = new JTextField(15);
		Results = new JTextArea(20, 40);
		Results.setEditable(false);
		Scroll = new JScrollPane(Results);
		Scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		// Text

		// Panels------------------

		// North
		NorthPanel = new JPanel();
		NorthPanel.setLayout(new GridLayout(0, 3));

		// Middle
		MiddlePanel = new JPanel();
		MiddlePanel.setBorder(new TitledBorder("Results"));
		MiddlePanel.add(Scroll);
		MiddlePanel.add(SummaryByTable);
		MiddlePanel.add(textField);

		// South
		SouthPanel = new JPanel();
		SouthPanel.setLayout(new GridLayout(1, 0));
		SouthPanel.add(Frequency);
		SouthPanel.add(Summary);
		SouthPanel.add(NotOrdered);

		// Panels -------------------
		MGUI.setDefaultCloseOperation(EXIT_ON_CLOSE);
		MGUI.add(MiddlePanel);
		MGUI.setSize(500, 500);
		MGUI.setVisible(true);
		MGUI.add(MiddlePanel);
		MGUI.add(SouthPanel, BorderLayout.SOUTH);
	}

	/**
	 * GUI ActionListeners
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == Frequency) {
			Results.setText(manager.GUIFrequency());
		} else if (e.getSource() == Summary) {
			Results.setText(manager.GUISummary());
		} else if (e.getSource() == SummaryByTable) {
			if (textField.getText().isEmpty() == false) {
				try {
					Results.setText(manager.GUISelectedSummary(Integer
							.parseInt(textField.getText())));
				} catch (NumberFormatException ex) {
					JOptionPane.showMessageDialog(null, "Enter numbers only.",
							"Oops!", JOptionPane.WARNING_MESSAGE);
					// System.err.print("Enter numbers only");
				} catch (NullPointerException enull) {
					JOptionPane.showMessageDialog(null,
							"Table does not exist.", "Oops!",
							JOptionPane.WARNING_MESSAGE);
					// System.err.print("Table Does Not Exist");
				}
			} else if (textField.getText().isEmpty() == true) {
				JOptionPane.showMessageDialog(null, "Enter a table number.",
						"Oops!", JOptionPane.WARNING_MESSAGE);
				// textfield.setText("Enter a table number");
			}

		} else if (e.getSource() == NotOrdered) {
			Results.setText(manager.GUINotOrderd());
		}
	}
}